/*
 * Copyright (C) 2024-2025 Deepin Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "loginsafetymodel.h"
#include "invokers/invokerfactory.h"
#include "common.h"

LoginSafetyModel::LoginSafetyModel(QObject *parent)
    : QObject(parent)
    , m_gSettingsInvokerInter(nullptr)
    , m_loginSafetyInvokerInter(nullptr)
    , m_controlCenterInvokerInter(nullptr)
    , m_netMonitorInvokInter(nullptr)
{
    m_gSettingsInvokerInter = InvokerFactory::GetInstance().CreateSettings(DEEPIN_DEFENDER_GSETTING_PATH, "", this);
    m_loginSafetyInvokerInter = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.LoginSafety",
                                                                            "/com/deepin/defender/LoginSafety",
                                                                            "com.deepin.defender.LoginSafety",
                                                                            ConnectType::SYSTEM, this);
    m_controlCenterInvokerInter = InvokerFactory::GetInstance().CreateInvoker("com.deepin.dde.ControlCenter",
                                                                              "/com/deepin/dde/ControlCenter",
                                                                              "com.deepin.dde.ControlCenter",
                                                                              ConnectType::SESSION, this);
    m_netMonitorInvokInter = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.MonitorNetFlow",
                                                                         "/com/deepin/defender/MonitorNetFlow",
                                                                         "com.deepin.defender.MonitorNetFlow",
                                                                         ConnectType::SYSTEM, this);

    m_loginSafetyInvokerInter->Connect("SetPwdLimitedLevelFinished", this, SLOT(onSetPwdLimitedLevelFinished(int)));

    m_setting = new def::SystemSettings(this);
    connect(m_setting, SIGNAL(valueChanged(const QString &, const QVariant &)), this, SLOT(onSettingChanged(const QString &, const QVariant &)));
}

LoginSafetyModel::~LoginSafetyModel()
{
}

// 开启/关闭密码限制策略
void LoginSafetyModel::setPwdLimitPolicyEnable(const bool &enable)
{
    DBUS_NOBLOCK_INVOKE(m_loginSafetyInvokerInter, "SetPwdLimitPolicyEnable", enable);
}

// 获取登录安全等级
int LoginSafetyModel::getPwdLimitLevel() const
{
    QDBusMessage msg = DBUS_BLOCK_INVOKE(m_loginSafetyInvokerInter, "GetPwdLimitedLevel");
    GET_MESSAGE_VALUE(int, level, msg);
    return level;
}

// 设置登录安全等级
void LoginSafetyModel::setPwdLimitLevel(const int level)
{
    DBUS_NOBLOCK_INVOKE(m_loginSafetyInvokerInter, "SetPwdLimitedLevel", level);
}

// 获取密码到期提醒截止天数类型
int LoginSafetyModel::getPwdChangeDeadlineType() const
{
    return m_gSettingsInvokerInter->GetValue(PWD_CHANGE_DEADLINE_TYPE).toInt();
}

// 设置密码到期提醒截止天数类型
void LoginSafetyModel::setPwdChangeDeadlineType(const int &type)
{
    m_gSettingsInvokerInter->SetValue(PWD_CHANGE_DEADLINE_TYPE, type);

    // 添加日志
    QString logStr;
    switch (type) {
    case 0:
        logStr = tr("Set \"notify me before password expired\" to: 30 days");
        break;
    case 1:
        logStr = tr("Set \"notify me before password expired\" to: 60 days");
        break;
    case 2:
        logStr = tr("Set \"notify me before password expired\" to: 90 days");
        break;
    case 3:
        logStr = tr("Set \"notify me before password expired\" to: 120 days");
        break;
    case 4:
        logStr = tr("Set \"notify me before password expired\" to: 150 days");
        break;
    case 5:
        logStr = tr("Set \"notify me before password expired\" to: 180 days");
        break;
    case 6:
        logStr = tr("Set \"notify me before password expired\" to: Never");
        break;
    default:
        logStr = tr("Set \"notify me before password expired\" to: Never");
        break;
    }
    DBUS_BLOCK_INVOKE(m_netMonitorInvokInter, "AddSecurityLog", SECURITY_LOG_TYPE_TOOL, logStr);
}

// 打开控制中心账号页
void LoginSafetyModel::jumpToAccountSettingPage()
{
    DBUS_NOBLOCK_INVOKE(m_controlCenterInvokerInter, "ShowPage", "accounts", "");
}

void LoginSafetyModel::onSetPwdLimitedLevelFinished(int level)
{
    // 添加日志
    QString logStr; // 日志文字
    switch (static_cast<PwdLimitLevel>(level)) {
    case Medium:
        logStr = tr("Password policy was set to: Medium");
        break;
    case High:
        logStr = tr("Password policy was set to: High");
        break;
    default:
        level = PwdLimitLevel::Low;
        logStr = tr("Password policy was set to: Low");
        break;
    }
    DBUS_BLOCK_INVOKE(m_netMonitorInvokInter, "AddSecurityLog", SECURITY_LOG_TYPE_TOOL, logStr);

    // 发送信号
    Q_EMIT setPwdLimitedLevelFinished(level);
}

void LoginSafetyModel::onSettingChanged(const QString &key, const QVariant &value)
{
    if (key == SYSTEM_SAFE_LEVEL) {
        int level = value.toInt();
        Q_EMIT systemSecurityLevelChanged(level);
    }
}

void LoginSafetyModel::syncSystemSecurityLevel()
{
    int level = m_setting->getValue(SYSTEM_SAFE_LEVEL).toInt();
    Q_EMIT systemSecurityLevelChanged(level);
}
